<?php
class controller_news extends model_news
{
    public function category()
    {
        $datas = $this->mod_category_list();
        $this->display('news/category', array(
            'datas' => $datas,
            'basepath' => '/news/category',
        ));
    }

    /*
    * view 添加分类
    */
    public function category_add()
    {
        $this->display('news/category_add', array(
            'parentcategory' => $this->mod_news_category(),
            'basepath' => '/news/category',
            'maxsort' => $this->table_maxsort('@@news_category', 'sort', '1'),
        ));
    }

    /*
    * view 编辑分类
    */
    public function category_edit()
    {
        $id = $this->existsKeyInArray('id',$_GET,'N');
        $parentcategory = $this->mod_news_category();
        $data = $this->getRow('@@news_category','*', array('id'=>$id));
        $this->display('news/category_edit', array(
            'basepath' => '/news/category',
            'editid' => $id,
            'data' => $data,
            'parentcategory' => $parentcategory,
        ));
    }

    /*
    * 分类添加或编辑参数处理
    */
    private function category_add_edit_args()
    {
        $para = $_POST;
        $args = array(
            'id' => $this->existsKeyInArray('id', $para, 'N'),
            'title' => $this->existsKeyInArray('title', $para, 'S'),
            'explain' => $this->existsKeyInArray('explain', $para, 'S'),
            'parentid' => $this->existsKeyInArray('parentid', $para, 'S'),
            'icon' => $this->existsKeyInArray('icon', $para, 'S', '#'),
            'url' => $this->existsKeyInArray('url', $para, 'S', '#'),
            'state' => $this->existsKeyInArray('state', $para, 'N'),
            'sort' => $this->existsKeyInArray('sort', $para, 'N'),
        );
        if (empty($args['title'])) core::error("分类名称不能为空");
        return $args;
    }

    /*
    * 添加分类-保存
    */
    public function category_add_save()
    {
        if (!$this->post) core::__403();
        $para = $this->category_add_edit_args();
        unset($para['id']);
        $insertid = $this->table_row_save('@@news_category', $para);
        if (!$insertid) core::error("分类[ {$para['title']} ]添加失败");
        $this->addLog($this->authid,__FUNCTION__,$insertid,$para['title']);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "添加[" . $para['title'] . "]成功",
            'auto' => true,
            'yes' => array(
                'text' => '继续',
            ),
            'yes' => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/news/category");',
            ),
        ));
    }

    /*
    * 编辑分类 - 保存
    */
    public function category_edit_save()
    {
        if (!$this->post) core::__403();
        $para = $this->category_add_edit_args(true);
        $update_args = array('id' => intval($para['id']));
        unset($para['id']);
        $updateid = $this->table_row_save('@@news_category', $para, $update_args, true);
        if (!$updateid) core::error("更新[" . $para['title'] . "]失败");
        $this->addLog($this->authid,__FUNCTION__,$update_args['id'],$para['title']);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "更新[" . $para['title'] . "]成功",
            'auto' => true,
            'yes' => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/news/category");',
            ),
        ));
    }
    /*
     * 删除分类
     */
    public function category_delete(){
        $id = $this->existsKeyInArray('id',$_GET,'N');
        if(!$id) core::__403();
        //删除文章分类
        $sql = 'DELETE FROM `@@news_category` WHERE `id`='.$id;
        $delid = $this->db->query($sql,0);
        if($delid){
            //删除分类底下的文章
            $sql = 'DELETE FROM `@@news` WHERE `category`='.$id;
            $delid = $this->db->query($sql,0);
            if (!$delid) core::error("分类底下的文章删除失败");
        }else{
            core::error('分类删除失败');
        }
        $this->addLog($this->authid,__FUNCTION__,$id,'分类'.$id.'及分类下的文章被删除');
        $this->dialogue(array(
            'type' => 'success',
            'text' => "分类删除成功",
            'auto' => true,
            'yes' => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/news/category");',
            ),
        ));

    }
    /*
    * 分类状态变更 通过 GET 方式传送参数
    * @param $id int 商品分类的ID
    */
    public function category_state()
    {
        $id = intval($_GET['id']);
        $updateid = $this->update_state('@@news_category', $id);
        if (!$updateid) core::error("状态更新失败");
        $this->addLog($this->authid,__FUNCTION__,$updateid,'');
        $this->dialogue(array(
            'type' => 'success',
            'text' => "状态更新成功",
            'auto' => true,
            'yes' => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/news/category");',
            ),
        ));
    }

    //////////////////////////////////////////////////////////////////////////////////////
    /// 新闻管理
    /// /////////////////////////////////////////////////////////////////////////////////
    /*
    * 列表-参数采集过滤
    * @param bool $get 提交方式$_GET或$_POST
    */
    private function news_get_args($get = true)
    {
        $para = $get ? $_GET : $_POST;
        $args = array();
        $args['sKey'] = $this->existsKeyInArray('sKey', $para, 'S');
        $args['categoryid'] = $this->existsKeyInArray('categoryid', $para, 'N');
        $args['categorysubid'] = $this->existsKeyInArray('categorysubid', $para, 'N');
        $args['ps'] = $this->existsKeyInArray('ps', $para, 'N', $this->pagesize);
        return $args;
    }
    /*
     * 文章列表页面参数传送处理
     */
    private function news_page_args($args)
    {
        $page_args = array();
        if (!empty($args['sKey'])) $page_args['sKey'] = $args['sKey'];
        if (!empty($args['categoryid'])) $page_args['categoryid'] = $args['categoryid'];
        if (!empty($args['categorysubid'])) $page_args['categorysubid'] = $args['categorysubid'];
        if (!empty($args['ps'])) $page_args['ps'] = $args['ps'];
        $page_args['page'] = '{page}';
        return $page_args;
    }

    /*
     * 列表
     */
    public function news()
    {
        $args = $this->news_get_args(true);
        $page_args = $this->news_page_args($args);
        $args['page_current'] = $this->get_page();

        $datas = $this->mod_news_list($args);
        $args['page_max'] = $this->get_maxpage($args['ps'], $datas['record']);

        $basepath = '/news/news';
        $categorys = $this->mod_news_category();

        $this->display("news/news_index", array(
            'basepath' => $basepath,
            'category' => $categorys,
            'datas' => $datas['datas'],
            'record' => $datas['record'],
            'page_max' => $args['page_max'],
            'page_current' => $args['page_current'],
            'page_url' => http_build_query($page_args),
            'extra' => '',
            'searchdata' => $args,
            'container' => '#iframe'
        ));
    }

    /*
    * 状态更新
    */
    public function news_state()
    {
        $id = $this->existsKeyInArray('id', $_REQUEST, 'N');
        if (!$id) core::error('参数错误');
        $updateid = $this->update_state('@@news', $id);
        $this->addLog($this->authid,__FUNCTION__,$id,'');
        $this->dialogue(array(
            'type' => 'success',
            'text' => "状态更新成功",
            'auto' => true,
            'yes' => array(
                'text' => '知道了',
            ),
        ));
    }

    /*
    * 删除选定
    */
    public function news_delete_list()
    {
        if (!$this->post) core::__403();
        $ids = $this->existsKeyInArray('ids', $_POST, 'S');
        if (empty($ids)) core::error('没有选择要删除的数据行');
        $delid = $this->mod_news_delete_list($ids);
        if (!$delid) core::error('删除失败');
        $this->dialogue(array(
            'type' => 'success',
            'text' => "操作成功",
            'auto' => true,
            'yes' => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/news/news");',
            ),
        ));
    }
    /*
     * 删除指定ID的文章 GET ID
     */
    public function news_delete()
    {
        $id = $this->existsKeyInArray('id', $_GET, 'N');
        if (!$id) core::__403();
        $delid = $this->mod_news_delete($id);
        if (!$delid) core::error('删除失败');
        $this->addLog($this->authid,__FUNCTION__,$id,'');
        $this->dialogue(array(
            'type' => 'success',
            'text' => "操作成功",
            'auto' => true,
            'yes' => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/news/news");',
            ),
        ));
    }
    /*
     * 添加文章
     */
    public function news_add()
    {
        $this->display("news/news_add", array(
            'basepath' => '/news/news',
            'category' => $this->mod_news_category(),
            'maxsort' => $this->table_maxsort('@@news', 'sort', '1'),
        ));
    }
    /*
     * 编辑文章
     */
    public function news_edit()
    {
        $id = $this->existsKeyInArray('id', $_GET, 'N');
        if (!$id) core::__403();
        $this->display("news/news_edit", array(
            'basepath' => '/news/news',
            'category' => $this->mod_news_category(),
            'data' => $this->mod_news_item($id),
            'content' => $this->getRow('@@news_content','content', array('newsid'=>$id)),
        ));
    }
    /*
     * 添加或保存文章的参数处理
     */
    private function news_add_edit_args()
    {
        $para = $_POST;
        $args = array(
            'id' => $this->existsKeyInArray('id', $para, 'N'),
            'title' => $this->existsKeyInArray('title', $para, 'S'),
            'url' => $this->existsKeyInArray('url', $para, 'S'),
            'icon' => $this->existsKeyInArray('icon', $para, 'S'),
            'icon1' => $this->existsKeyInArray('icon1', $para, 'S'),
            'icon2' => $this->existsKeyInArray('icon2', $para, 'S'),
            'isbig' => $this->existsKeyInArray('isbig', $para, 'S'),
            'explain' => $this->existsKeyInArray('explain', $para, 'S'),
            'content' => $this->existsKeyInArray('content', $para, 'S'),
            'category' => $this->existsKeyInArray('category', $para, 'N'),
            'actiontime' => $this->existsKeyInArray('actiontime', $para, 'S'),
            'validity' => $this->existsKeyInArray('validity', $para, 'S'),
            'subtitle' => $this->existsKeyInArray('subtitle', $para, 'S'),
            'reading' => $this->existsKeyInArray('reading', $para, 'N'),
            'state' => $this->existsKeyInArray('state', $para, 'N'),
            'mastlogin' => $this->existsKeyInArray('mastlogin', $para, 'N'),
            'sort' => $this->existsKeyInArray('sort', $para, 'N'),
        );
        if (empty($args['title'])) core::error('文章标题不能为空');
        $args['actiontime'] = strtotime($args['actiontime']);
        //统计图片数
        $counticon = 0;
        if ($args['icon'] != '#' && !empty($args['icon'])) $counticon++;
        if ($args['icon1'] != '#' && !empty($args['icon'])) $counticon++;
        if ($args['icon2'] != '#' && !empty($args['icon'])) $counticon++;
        $args['counticon'] = $counticon;
        return $args;
    }
    /*
     * 保存添加的文章
     */
    public function news_add_save()
    {
        if (!$this->post) core::__403();
        $args = $this->news_add_edit_args();
        $content = $args['content'];
        $imgsurl = $this->config['imgsurl'];
        $content = str_replace('/upload/',$imgsurl,$content);
        unset($args['id']);
        unset($args['content']);
        //$update_args = array('id'=>intval($args['id']));
        $insertid = $this->table_row_save('@@news', $args);
        $this->table_row_save('@@news_content', array('newsid'=>$insertid, 'content'=>$content));
        if (!$insertid) core::error("文章[ {$args['title']} ]添加失败");
        $this->addLog($this->authid,__FUNCTION__,$insertid,$args['title']);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "文章[ {$args['title']} ]添加成功",
            'auto' => true,
            'yes' => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/news/news");',
            ),
        ));
    }
    /*
     * 保存编辑后的文章
     */
    public function news_edit_save()
    {
        if (!$this->post) core::__403();
        $args = $this->news_add_edit_args();
        $editid = intval($args['id']);
        $update_args = array('id' => $editid);
        unset($args['id']);
        $content = $args['content'];
        $imgsurl = $this->config['imgsurl'];
        $content = str_replace('/upload/',$imgsurl,$content);
        unset($args['id']);

        unset($args['content']);
        $updateid = $this->table_row_save('@@news', $args, $update_args, true);
        $this->table_row_save('@@news_content', array('content'=>$content), array('newsid'=>$editid), true);
        //$sql = "REPLACE INTO @@news_content(`newsid`,`content`) VALUES (".$editid.", '".$content."')";
        //"UPDATE @@news_content set ";
        //$this->db->query($sql, 1);
        //if (!$updateid) core::error("文章[ {$args['title']} ]更新失败");
        $this->addLog($this->authid,__FUNCTION__,$update_args['id'],$args['title']);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "文章[ {$args['title']} ]更新成功",
            'auto' => true,
            'yes' => array(
                'text' => '返回',
                'func' => 'JumpMainFrame("/news/news");',
            ),
        ));
    }
    /////////////////////////////////////////////////////////////////////////////////////

}